<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.homeId ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
              <el-form-item label="房间号" prop="homeName">
          <el-input v-model="homeName" placeholder="房间号" :readonly="true"></el-input>
      </el-form-item>
      <el-form-item label="订/退房" prop="dictId">
        <el-input v-model="dictName" placeholder="订/退房" :readonly="true"></el-input>
      </el-form-item>
      <el-form-item label="到店时间" prop="startHireDate">
        <el-date-picker
            onClick="getDifferTime"
            v-model="dataForm.startHireDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
            default-time="12:00:00">
        </el-date-picker>
      </el-form-item>
          <el-form-item label="价格" prop="dayMoney">
          <el-input v-model="dataForm.dayMoney" placeholder="价格" :readonly="true"></el-input>
      </el-form-item>
               <el-form-item label="住宿天数" prop="inDay">
              <el-input v-model="dataForm.inDay" placeholder="住宿天数"></el-input>
           </el-form-item>
<!--          <el-form-item label="总金额" prop="totalMoney">-->
<!--          <el-input v-model="dataForm.totalMoney" placeholder="总金额"></el-input>-->
<!--      </el-form-item>-->
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <template>
        <el-button type="primary" @click="open">{{ $t('confirm') }}</el-button>
      </template>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      AddRoomUserVisible: false,
      visible: false,
      dictIds:[],
      homeName: '',
      dictName:'',
      dataForm: {
        id: '',
        creator: '',
        createDate: '',
        homeId: '',
        dictId: '',
        startHireDate: '',
        endHireDate: '',
        dayMoney: '',
        totalMoney: '',
        userId:'',
        inDay:''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        homeId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        dictId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        dayMoney: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        totalMoney: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        userId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getDictId();
    this.getHomeName();
  },
  methods: {
    open() {
      var  startDate=this.dataForm.startHireDate
      var endDate = this.dataForm.endHireDate
      if(startDate!==null&&endDate!==null){
        let startTime = new Date(Date.parse(startDate.replace(/-/g, "/"))).getTime();
        let endTime = new Date(Date.parse(endDate.replace(/-/g, "/"))).getTime();
        let dates = Math.abs((startTime - endTime)) / (1000 * 60 * 60 * 24);
        this.dataForm.totalMoney=this.dataForm.dayMoney* this.dataForm.inDay+100}
      this.$prompt('请输入总金额'+this.dataForm.totalMoney, '提示,内含押金100', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValidator:this.dataForm.totalMoney,
        inputErrorMessage: '金额不正确'
      }).then(({ value }) => {
        if(value===this.dataForm.totalMoney.toString()){
          this.$message({
            type: 'success',
            message: '您已支付: ' + value,

          });
          this.dataFormSubmitHandle()
        }else {
          this.$message({
            type: 'info',
            message: '支付错误,请重新支付'
          });
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消支付'
        });
      });
    },
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
      this.getHomeName()
      this.getDictId()
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/biz/roomrecord/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/biz/roomrecord/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false }),
    getDictId: function () {
      let dictTypeId = "1623523739133562882";
      this.$http.get(`/sys/dict/data/getDataByPid?dictTypeId=` + dictTypeId).then(
          ({data: res}) => {
            this.dictIds = res.data
          })
    },
    getHomeName: function () {
      this.$http.get(`/biz/roominformation/` +this.dataForm.homeId).then(
          ({data: res}) => {
            this.homeName = res.data.roomNumber
            this.dataForm.dayMoney = res.data.price
            if(res.data.state==="1"){
              this.dictName="订房"
              this.dataForm.dictId="1623523999075553281"
            }else {
              this.dictName="退房"
              this.dataForm.dictId="1623524059653885953"
            }
          })
    },
  }
}
</script>
